package com.elangzhi.ssm.modules.goodsComment.app;

import com.elangzhi.ssm.controller.json.Tip;
import com.elangzhi.ssm.model.GoodsComment;
import com.elangzhi.ssm.modules.goodsComment.services.GoodsCommentService;
import com.elangzhi.ssm.tools.UUIDFactory;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.Date;

/**
* 商品评价 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/app/goodsComment")
public class AppGoodsCommentController {


    /**
     * 获取商品评论 分页
     * @param goodsId 商品ID
     * @param page 当前页
     * @param size 每页大小
     * @return 评论列表
     */
    @RequestMapping("listByGoodsId")
    @ResponseBody
    public PageInfo<GoodsComment> listByShopId(
                                           @RequestParam("goodsId") Long goodsId,
                                           @RequestParam("page") Integer page,
                                           @RequestParam("size")  Integer size){
        return goodsCommentService.listByGoodsId(goodsId,page,size);
    }

    /**
     * 添加评论
     * @param goodsComment 参数实体
     * @return 状态
     * 1，参数无效
     * 2，添加失败
     */
    @RequestMapping("save")
    @ResponseBody
    public Tip save(GoodsComment goodsComment){
        if(goodsComment != null){
            goodsComment.setId(UUIDFactory.getLongId());
            goodsComment.setType(1);
            goodsComment.setSetTime(new Date());
            try {
                goodsCommentService.insert(goodsComment);
                return new Tip(goodsComment);
            } catch (Exception e) {
                e.printStackTrace();
                return new Tip(2);
            }
        }else{
            return new Tip(1);
        }
    }




    //---------------------------- property -------------------------------

    @Resource
    private GoodsCommentService goodsCommentService;

}
